package com.briup.cms.web.controller;

import com.briup.cms.bean.Article;
import com.briup.cms.bean.BaseUser;
import com.briup.cms.bean.Category;
import com.briup.cms.service.IArticleService;
import com.briup.cms.utils.Message;
import com.briup.cms.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@Api(tags = "资讯管理相关接口")
@RequestMapping("/auth/article")
@RestController
public class ArticleController {

    @Autowired
    private IArticleService service;

    @ApiOperation("通过Id查询咨询信息")
    @ApiImplicitParam(name="id",value = "需要查询的id",dataType = "int")
    @GetMapping("/findById")
    public Message findByIdWithCategoryAndUser(int id){
        return MessageUtil.success(service.findByIdWithCategoryAndUser(id));
    }

    @ApiOperation("分页查询所有的资讯信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",dataType = "int",required = true),
            @ApiImplicitParam(name = "title",value = "标题",dataType = "String"),
            @ApiImplicitParam(name = "status",value = "状态",dataType = "Integer"),
            @ApiImplicitParam(name = "categoryId",value = "栏目Id",dataType = "Integer")

    })
    @GetMapping("/page/queryWithCategoryAndUser")
    public Message pageQueryArticleWithCategoryAndUser(int pageNum, int pageSize, String title, Integer status, Integer categoryId){
        return MessageUtil.success(service.pageQueryArticleWithCategoryAndUser(pageNum, pageSize, title, status, categoryId));
    }

    @ApiOperation("分页获取所有推荐的资讯信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",dataType = "int",required = true)
    })
    @GetMapping("/page/query/recommend")
    public Message pageQueryRecommend(int pageNum, int pageSize){
        return MessageUtil.success(service.pageQueryRecommend(pageNum,pageSize));
    }

    @ApiOperation("保存资讯信息")
    @PostMapping("/saveOrUpdate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId",value = "编号",dataType = "Integer"),
            @ApiImplicitParam(name = "articleTitle",value = "标题",dataType = "String"),
            @ApiImplicitParam(name = "articleContent",value = "内容",dataType = "String"),
            @ApiImplicitParam(name = "articlePublishTime",value = "发布时间",dataType = "Date"),
//            @ApiImplicitParam(name = "article_read_times",value = "阅读次数",dataType = "Integer"),
            @ApiImplicitParam(name = "articleStatus",value = "状态：0-待审核，1-不通过，2-通过，3-推荐",dataType = "Integer"),
//            @ApiImplicitParam(name = "articleThumpup",value = "点赞数量",dataType = "Integer"),
            @ApiImplicitParam(name = "articleCover",value = "封面图片地址",dataType = "String"),
            @ApiImplicitParam(name = "articleCategoryId",value = "所属用户id",dataType = "Integer"),
            @ApiImplicitParam(name = "articleUserId",value = "所属栏目id",dataType = "Integer")
    })
    public Message saveOrUpdate(
            Integer articleId,
            String articleTitle,
            String articleContent,
            Date articlePublishTime,
//            int article_read_times,
            Integer articleStatus,
//            Integer articleThumpup,
            String articleCover,
            Integer articleCategoryId,
            Integer articleUserId
    ){
        Article article = new Article();
        article.setArticleId(articleId);
        article.setArticleTitle(articleTitle);
        article.setArticleContent(articleContent);
//        article.setArticlePublishTime(articlePublishTime);
        article.setArticleStatus(articleStatus);
        article.setArticleCover(articleCover);
        Category c = new Category() ;
        c.setCategoryId(articleCategoryId);
        BaseUser user = new BaseUser();
        user.setUserId(articleUserId);
        article.setCategory(c);
        article.setBaseUser(user);


        service.saveOrUpdate(article);
        return MessageUtil.success(article);
    }

    @ApiOperation("通过Id删除资讯信息")
    @ApiImplicitParam(name="id",value = "需要删除的id",dataType = "int")
    @DeleteMapping("/deleteById")
    public Message deleteById(int id){
        service.deleteById(id);
        return MessageUtil.success();
    }

    @PutMapping("/changeStatus")
    @ApiOperation("修改资讯状态")
    public Message changeStatus(int id, int status){
        service.changeStatus(id,status);
        return MessageUtil.success();
    }

}
